<?php $this->loadHelper('common'); ?>
<div id="manager">
    <?php echo $this->renderElement('sidebar/video.tpl.php', array('_module' => $_module)); ?>
    <div class="right">
        <div>
            <div id="bg-title1"><img src="/images/bg-title1.jpg" alt="" /></div>
            <div id="bg-title2a"><img src="/images/icon-title.jpg" style="margin-right:8px;" alt="" />Tìm kiếm Video</div>

            <div id="bg-title3"><img src="/images/bg-title3.jpg" alt="" /></div>
            <div class="clearfloat"></div>
        </div>
        <div id="content">
            <div class="area-button">
                <form name="frmSearch" action="" method="GET">
                <div class="area-button1">
                    <a class="button" href="javascript:;" onclick="document.frmSearch.submit();return false;">Tìm</a>
                    Tìm kiếm theo
                    <select name="search_type">
                        <option value="name" <?php echo isset($_GET['search_type']) && $_GET['search_type']=='name' ? 'selected' : ''; ?>>Tên</option>
                        <option value="id" <?php echo isset($_GET['search_type']) && $_GET['search_type']=='id' ? 'selected' : ''; ?>>ID</option>
                    </select>
                    <input type="text" class="textbox" style="width:450px" name="keyword" value='<?php echo isset($keyword) ? htmlentities(strip_tags($keyword), ENT_QUOTES, "UTF-8") : ''; ?>' />
                </div>
                </form>
                <div class="clearfloat"></div>
            </div>            
            <?php foreach ($videoList as $video): ?>
                <?php
                $videoStatus = 'không rõ!';
                $converted  = $video['converted'];
                $active     = $video['active'];
                $deleted    = $video['deleted'];

                $isPending      = $converted==1 && !$active && !$deleted;
                $isActive       = $converted==1 && $active && !$deleted;
                $isDeleted      = $deleted;
                $isNotConverted = $converted==0;
                $isConverted    = $converted==1;
                $isConvertFail  = $converted==-1;

                $updated_by_admin = ($video['member_id'] != $video['updated_by']);

                $pending = false;
                if ($converted==1 && $active==1 && $deleted==0) {
                    $videoStatus = 'đã kích hoạt';
                    $pending = true;
                } else if ($converted==1 && $active==0 && $deleted==0) {
                        $videoStatus = 'đang chờ kích hoạt';
                    } elseif ($converted==1 && $active==1 && $deleted==1 && $updated_by_admin) {
                        $videoStatus = 'bị từ chối (decline)';
                    } elseif ($converted==1 && $active==1 && $deleted==1 && !$updated_by_admin) {
                        $videoStatus = 'đã kích hoạt nhưng CP xóa';
                    } elseif ($converted==-1 && $deleted==0) {
                        $videoStatus = 'convert bị lỗi';
                    } elseif ($converted==-1 && $deleted==1 && $updated_by_admin) {
                        $videoStatus = 'convert lỗi, đã bị quản trị xóa';
                    } elseif ($converted==-1 && $deleted==1 && !$updated_by_admin) {
                        $videoStatus = 'convert lỗi, đã bị CP xóa!';
                    } elseif ($converted==0 && $deleted==0) {
                        $videoStatus = 'chưa convert';
                    } elseif ($converted==0 && $deleted==1 && $updated_by_admin) {
                        $videoStatus = 'chưa convert đã bị quản trị xóa!';
                    } elseif ($converted==0 && $deleted==1 && !$updated_by_admin) {
                        $videoStatus = 'chưa convert đã bị CP xóa!';
                    } elseif ($converted==1 && $deleted==1 && $active==0 && $updated_by_admin) {
                        $videoStatus = 'đang chờ duyệt thì quản trị xóa!';
                    } elseif ($converted==1 && $deleted==1 && $active==0 && !$updated_by_admin) {
                        $videoStatus = 'đang chờ duyệt thì CP xóa!';
                    }
                ?>
            <div class="area-content1">
                <div class="video1"><input type="checkbox" id="item-<?php echo $video['id'] ?>" value="<?php echo $video['id'] ?>" /></div>
                <div class="video2">
                        <?php if($converted == 1): ?>
                            <?php if(!$pending): ?>
                    <a href="/video/edit?id=<?php echo $video['id'] ?>"><img src="<?php echo makeVideoThumbnailUrl($video['id'], $video['thumbnail_version']); ?>" width="128" height="96" alt="Ảnh thumbnail" /></a>
                            <?php else: ?>
                    <img src="<?php echo makeVideoThumbnailUrl($video['id'], $video['thumbnail_version']); ?>" width="128" height="96" alt="Ảnh thumbnail" />
                            <?php endif; ?>
                        <?php else: ?>
                    <img src="/images/processing.jpg" alt="" width="128" height="96"/>
                        <?php endif; ?>
                </div>
                <div class="video3">
                        <?php if(!$pending):?>
                    <a href="/video/edit?id=<?php echo $video['id'] ?>" class="style_title"><?php echo $video['title']; ?></a> <?php if($video['hd']): ?><img src="/images/hd_icon.gif" /><?php endif;?> <?php if($video['source']=='nn'): ?><img src="/images/eng_icon.gif" /><?php endif;?>
                        <?php else: ?>
                            <?php echo $video['title']; ?>
                        <?php endif; ?>
                    <br />
                        <?php echo truncate(strip_tags($video['description']), 150); ?><br />
                </div>
                <div class="video4">
                    Đăng ngày: <?php echo timeFormat($video['created_time']); ?><br />
                    Đăng bởi: <b><?php echo $video['owner']; ?></b><br/>
                    <?php if($video['subcp_name']): ?>
                    Đối tác: <b><?php echo $video['subcp_name']; ?></b><br/>
                    <?php endif; ?>
                    Giá xem: <?php echo $video['price']; ?><br />
                    Giá tải: <?php echo $video['download_price']; ?><br />
                    Chuyên mục: <?php echo $video['category_name']; ?><br/>
                    Trạng thái: <?php echo $videoStatus; ?><br />
                    Độ dài: <?php echo secondToTime($video['duration']); ?><br />
                    Xem: <?php echo $video['watch_count']; ?> - Tải: <?php echo $video['download_count']; ?> - Tặng: <?php echo $video['present_count']; ?><br/>
                </div>
                <div class="clearfloat"></div>

                <div class="video5">
                        <?php if (($isPending || $isActive || $isDeleted) && $converted==1): ?>
                    <a href="#" class="button1" onclick="showVideo(<?php echo $video['id']; ?>);">Xem clip</a>
                        <?php endif; ?>

                    <a href="javascript:;" onclick="deleteObject('video',<?php echo $video['id']; ?>);return false;" class="button1">Xóa clip</a>

                        <?php if ($isActive): ?>
                    <a href="/video/edit?id=<?php echo $video['id']; ?>" class="button1">Sửa clip</a>
                        <?php endif; ?>

                        <?php if ($isPending): ?>
                    <a href="javascript:;" onclick="approveVideo(<?php echo $video['id']; ?>);return false;" class="button1">Chọn duyệt</a>
                        <?php endif; ?>

                        <?php if ($isConvertFail): ?>
                    <a href="#" class="button1">Convert lại</a>
                        <?php endif; ?>

                        <?php if ($isActive && !isset($featuredVideos[$video['id']])): ?>
                    <a href="/featured_video/add?id=<?php echo $video['id']; ?>" class="button1" title="Thêm vào sanh sách chọn lọc">+chọn lọc</a>
                        <?php elseif ($isActive && isset($featuredVideos[$video['id']])): ?>
                    <a href="/featured_video/delete?id=<?php echo $video['id']; ?>" class="button1" title="Bỏ ra danh sách chọn lọc">-chọn lọc</a>
                        <?php endif;
                    if(isAdmin ())?>
                        <a href="/behavior?admin=&object=video&object_id=<?=$video['id']?>" class="button1" title="Thống kê">Lịch sử</a>
                </div>

                <div class="clearfloat"></div>
            </div>

            <?php endforeach; ?>
            <?php if ($totalPage != 0): ?>
            <div class="area-button">
                <div style="float:right; padding-right:10px">Có tất cả <b><?php echo $totalRow; ?></b> video</div>
                <div class="clearfloat"></div>
            </div>
            <?php else: ?>
            <div class="area-content1">Không có video nào.</div>
            <?php endif; ?>
            <?php echo $this->renderElement('paging.tpl.php', array('page' => $curPage, 'totalPage' => $totalPage)); ?>
        </div>
    </div>
</div>
<div class="clearfloat"></div>

<script type="text/javascript">
    function showVideo(clipId) {
        $.get("/video/getplayer", { id: clipId }, function(data){
            $.modal(data);
        });
    }
</script>
